package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.imageio.spi.RegisterableService;
import java.util.List;
import java.util.logging.Level;

/**
 * @author 国兴越
 * @version 1.0
 * Create by
 */
@Slf4j
@Component
public class SpringCacheSynHandler {

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    IRegionService regionService;


    @Resource
    private HomeService homeService;


    /**
     * 每天凌晨一点 更新缓存
     */
    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync(){
        //定义key
        String key = "JZ_CACHE::ACTIVE_REGIONS";

        redisTemplate.delete(key);

        //遍历区域下的类型
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();

        regionSimpleResDTOS.forEach(item->{

            Long regionId = item.getId();


            String serve_type_key=RedisConstants.CacheName.SERVE_ICON+"::"+regionId;
            //删除首页下的区域列表
            redisTemplate.delete(serve_type_key);

            homeService.serveCacheList(regionId);


        });


    }


}
